/**
 * 
 */
package info.niwota.home.fm;

import info.niwota.home.R;
import x.java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author qiangli
 *
 */
public class DeleteActivity extends BaseFileActivity {


	private String path;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
		setTheme(android.R.style.Theme_Translucent_NoTitleBar);
		setContentView(R.layout.delete);

		init();
	}

	private void init() {
		final TextView pathText = (TextView) findViewById(R.id.delete_path);
		final Button confirmBtn = (Button) findViewById(R.id.delete_confirm);
		final Button cancelBtn = (Button) findViewById(R.id.delete_cancel);
		//
		Uri data = getIntent().getData();
		path = data.getPath();
		pathText.setText(path);
		//
		confirmBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.delete_confirm:
			doConfirm();
			break;
		case R.id.delete_cancel:
			doCancel();
			break;
		default:
		}
		finish();
	}
	
	private void doCancel() {
		setResult(RESULT_CANCELED);
	}

	private void doConfirm() {
		try {
			File f = new File(path);
			if (f.exists() == false) {
				Toast.makeText(this, "File does not exist. " + f,
						Toast.LENGTH_LONG).show();
				setResult(RESULT_CANCELED);
				return;
			}
			if ((f.isFile() || f.isLink() || f.isEmpty()) == false) {
				Toast.makeText(this, "Folder not empty. " + f,
						Toast.LENGTH_LONG).show();
				setResult(RESULT_CANCELED);
				return;
			}
			if (f.delete() == false) {
				Toast.makeText(this, "Failed to delete. " + f,
						Toast.LENGTH_LONG).show();
				setResult(RESULT_CANCELED);
				return;
			}
			//success
			Intent data = new Intent();
			data.setData(Uri.parse(f.toURI().toString()));
			setResult(RESULT_OK, data);
		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(),
					Toast.LENGTH_LONG).show();
			setResult(RESULT_CANCELED);
		}
	}
}
